# -*- coding: utf-8 -*-
from common.utils import format_float
from k3_data_report.business.cgtj_process import cgti_gys_main, cgti_pp_main
from k3_data_report.business.cgyjfk_process import create_df_cgyjfkrmb, create_df_cgyjfkusd, cgyjfk_gys_process_main
from k3_data_report.business.chtj_process import chtj_pp_main, chtj_kh_main
from k3_data_report.business.chycbj_process import create_df_chycbj
from k3_data_report.business.ddycbj_process import create_df_ddycbj, ddycbj_main
from k3_data_report.business.xstj_process import xstj_pp_main, xstj_kh_main
from k3_data_report.finance.cpbfy_process import cpbfy_main, create_df_cpbfy
from k3_data_report.finance.skjllrhs_process import create_df_skjllrhs, create_df_skfx
from k3_data_report.finance.zhmx_process import create_df_zhmx

__author__ = 'sunnychou'
__date__ = '2019/8/2 15:43'

from k3_data_report.import_tb.k3_caigou import caigou_process_main
from k3_data_report.import_tb.k3_fukd import fukuan_process_main
from k3_data_report.import_tb.k3_gongys import gongyinshang_process_main
from k3_data_report.import_tb.k3_waigou import waigoud_process_main
from k3_data_report.import_tb.k3_xsfp import xiaoshoufap_process_main


BASE_DRP_TABLE_DIR = r"D:\k3crm_data_report_project\base_data_report"   #基础报表上传存放位置（以服务器为准）

UPLOAD_TABLE_DIR = r'D:\k3crm_data_report_project\uploads'      #文件上传目录（服务器）

EMAIL_FILE_DIR = r"D:\k3crm_data_report_project\emails"  #邮件待发送，缓存的本地位置

LOG_FILE_DIR = r'D:\k3crm_data_report_project\logs'      #日志保存路径

##################### 1 基础表的导入配置   begin  ##################

LOCAL_DIR_SCAN = 1         #1---本地扫描     0----文件上传服务器端扫描
#上传文件场景下使用1

k3_base_table_fields={
    "k3_base_scan":{   #扫描表
            #表前缀:(table_name, sheet_name, key_sql, optype)   optype:1 ---> 原生sql， optype:2 ---> orm
            "采购发票":("k3_cgfp","购货发票_专用_序时簿", "cgfp_insert",1),
            "销售出库":("k3_xsckd", "销售出库序时簿", "xsckd_insert",1),
            "销售订单":("k3_xsdd", "销售订单序时簿","xsdd_insert",1),
            "收款单":("k3_skd", "收款单序时簿", "skd_insert",1),
            #orm table insert section
            "采购订单":("k3_caigoud","采购订单序时簿","cgdd_insert", 2),
            "付款单":("k3_fukd","付款单序时簿","fkd_insert", 2),
            "供应商":("k3_gongys","供应商","gongys_insert",2),
            "外购入库":("k3_waigourk","外购入库序时簿", "waigourk_insert", 2),
            "销售发票":("k3_xsfp","销售发票专用序时簿", "xsfp_insert",2),

    },
    "check_sql":{
        "insert_check_sql":"  SELECT   DISTINCT sjsj from %s WHERE sjsj='%s'",
        "checkwj_sql": "select wjj,wjmc,sjsj,sftq FROM k3wjsm",
    },
    'k3_jichu_tb': {     #key  sql
        #采购发票
        'cgfp_insert': "insert into k3_cgfp (pinpai,ggxh,yfkrq,ghdw,fph,shbz,bm,ywy,bb,hv,wldm,wlmc,dfkm,dw,sl,zhdwsl,wsdj,ddhsdj,slv,se,bhsje,yjcbfy,yfsj,jshj,gjzt,pzzh,jbdw,jbdwsl,ygjsl,ygjje,wgjsl,wgjje,bqgjsl,bqgjje,dddh,yddh,ydlx,wlcdm,cgfs,nlv,bhsjebwb,sebwb,jshjbwb,ywlx,hxbz,shrq,zkl,zke,zkebwb,sjhsdj,bjcbfybw,cydw,ph,jsfs,kprq,id,sjsj)"
                       " values ('%s','%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s','%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s')",
        #'yskmxb_insert': "insert into k3_yskmx(djrq,cwrq,djlx,djbh,mxlx,pzh,skfph,wlkm,zy,bm,ywy,bqys,bqss,qmye,id,sjsj)"
        #                 "values ('%s','%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s', '%s')",
        #销售出库
        'xsckd_insert': "insert into k3_xsckd(ckrq,skrq,shbz,ckdh,xsfs,ghdw,fhck,cpdm,cpcdm,cpmc,ggxh,dw,ph,sfsl,dwcb,cb,pzzh,gjzt,xsywlx,ygjsl,wgjsl,bqgjsl,dddh,yddh,ydlx,bm,ywy,zg,jhdz,shrq,kpsl,yfsl,jhdj,jhje,xsdj,xsje,sjhm,txm,khhth,xshth,khbm,pp,wsdj,wsje,slv,id,sjsj)"
                        " values ('%s','%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s', '%s', '%s', '%s')",
        #销售订单
        'xsdd_insert': "insert into k3_xsdd(pp,cpmc,shbz,gbbz,qdrq,ghdw,xsfs,jhfs,bh,dycs,gbrq,gbyy,bbh,gbr,bibie,jh,zg,bm,ywy,zd,jsfs,hlv,ggxh,dw,sl,zhdwsl,kcsl,wsdj,wsje,zklv,dwzke,zke,jbdw,jbdwsl,shr,jhrq,bz,cpcdm,hywgbbz,jyjhraq,dydm,dymc,jbdwcksl,yddh,ydlx,jsrq,aaa,shrq,fzsx,fzsxdm,hsdj,sjhsdj,sxse,jshj,hslv,zwlfhbb,yf,fpsjrdz,sjh,gsmc,nssbm,zcdz,dh,khyh,yhzh,xch,khbm,ms,sjxqjhrq,khhth,id,sjsj)"
                       " values ('%s','%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s', '%s', '%s', '%s')",
        'khry_insert': "insert into k3_khry(khmc,khjl,kf,xsjl,id,sjsj) "
                       " values ('%s','%s','%s','%s','%s','%s')",
        'skd_insert':"insert into k3_skd(zflx,zt,djh,djrq,cwrq,hsxmlb,khdm,kh,bmdm,bm,ywydm,ywy,djje,bb,shr,ydlx,yddh,dddh,jsssje,jszkje,pp,cpmc,ggxh,sl,hsdj,jssl,cpdm,jsfs,id,sjsj)"
                     " values('%s','%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s','%s','%s', '%s', '%s', '%s')",
        'gysry_insert': "insert into k3_gysry(pp,cpjl,cpgly,id,sjsj) "
                        " values ('%s','%s','%s','%s','%s')",
        'cpgly_insert': "insert into k3_cpgly(gys,cpgly,id,sjsj) values('%s','%s','%s','%s')",
        'kh_insert':"insert into k3_kh (dm,mc,qm,dz,dh,cz,jsfs,zyywy,shr,fj,tp,id,sjsj) values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",

        #orm tables insert
        "cgdd_insert":caigou_process_main,
        "fkd_insert":fukuan_process_main,
        "gongys_insert":gongyinshang_process_main,
        "waigourk_insert":waigoud_process_main,
        "xsfp_insert":xiaoshoufap_process_main,

    },
    "saomia_insert": "insert into k3wjsm(wjj,wjmc,sjsj,sftq,id) values ('%s','%s','%s','%s','%s')",

}

##################### 1 基础表的导入配置   end  ##################


####################  2 月数据表的计算配置   begin  #################
from k3_data_report.business.cgyjfk_process import cgyjfk_gys_process
from k3_data_report.finance.caigou_fkd import cal_caigou_fkd_process_main
from k3_data_report.finance.kh_business import cal_kh_process_main
from k3_data_report.finance.skjllrhs_process import skjllrhs_main
from k3_data_report.finance.xsbfy_process import xsbfy_main, create_df_xsbfy
from k3_data_report.finance.zhmx_process import zhmx_main
from k3_data_report.business.chycbj_process import chycbj_main

#处理月数据配置信息  说明:"yw_"开头的是业务中心配置，"cw_"开头的是财务中心配置。
# 后面的函数是指的只包含month月份的参数处理函数  eg: cal_caigou_fkd_process_main(month), 统一化处理，
# 每个处理逻辑里面要对月份进行判断，如果月份数据存在就不插入，不存在就可以插入
# 函数返回值：True--处理完成     False--处理异常
MONTH_PROCESS_DICT={
    #key：（处理月逻辑函数，对应的数据库表名称）
    "yw_cgyjfk":cgyjfk_gys_process_main,               #预计付款计划表统计
    "yw_chycbj":chycbj_main,                    #出货异常报警
    "yw_ddycbj":ddycbj_main,                    #订单异常报警
    "yw_xstj_pp":xstj_pp_main,                     #销售统计品牌
    "yw_xstj_kh":xstj_kh_main,                     #销售统计客户
    "yw_chtj_pp":chtj_pp_main,                 #出货统计品牌
    "yw_chtj_kh":chtj_kh_main,                 #出货统计客户
    "yw_cgti_gys":cgti_gys_main,             #采购统计供应商
    "yw_cgti_pp":cgti_pp_main,                      #采购统计品牌
    "cw_cgfkd":cal_caigou_fkd_process_main,       #采购付款单
    "cw_khjyqk":cal_kh_process_main,              #客户经营情况
    "cw_skjlrhs":skjllrhs_main,                   #收款奖励利润核算
    "cw_xsbfy":xsbfy_main,                        #销售部费用
    "cw_cpbfy":cpbfy_main,                        #产品部费用
    "cw_zhmx":zhmx_main,                            #资金账户明细

}

#################### 2 月数据表的计算配置   end  #################


################  3 合并数据报表配置  begin #######################

from k3_data_report.business.cgtj_run_dp import create_df_k3cgtjgy,create_df_k3cgtjpp
from k3_data_report.business.chtj_run_dp import create_df_k3chtjcp,create_df_k3chtjkh,create_df_k3chtjjl
from k3_data_report.business.xstj_run_dp import create_df_k3xstjcp,create_df_k3xstjkh,create_df_k3xstjxs

from k3_data_report.finance.caigou_fkd_dp import create_df_k3cgfkds_rmb, create_df_k3cgfkds_mj
from k3_data_report.finance.kh_business_dp import create_df_k3jyqks_rmb,create_df_k3jyqks_mj
from k3_data_report.finance.kh_xsqkfx_dp import create_df_xsqkfx_rmb, create_df_xsqkfx_mj
from k3_data_report.finance.kh_yskfx import create_df_kh_yskfx_rmb, create_df_kh_yskfx_mj

#业务中心配置信息
DRP_TJ_YW_PROCESS_DICT = {
    #key:[(create_dataframe_func, excel_name)]
    "yw_cgtj_dp":[(create_df_k3cgtjgy, "供应商采购额统计-按管理员分类"),
                  (create_df_k3cgtjpp, "供应商采购额统计-按品牌分类")],
    "yw_chtj_dp":[(create_df_k3chtjcp, "出货统计-产品分类"),
                  (create_df_k3chtjkh, "出货统计-客户分类"),
                  (create_df_k3chtjjl, "出货统计-客户经理")],
    "yw_xstj_dp":[(create_df_k3xstjcp, "销售订单统计-产品分类"),
                  (create_df_k3xstjkh,"销售订单统计-客户分类"),
                  (create_df_k3xstjxs, "销售订单统计-客户经理分类")],
    "yw_cgyjfk_dp":[(create_df_cgyjfkrmb,"采购预计付款计划表-人民币"),
                    (create_df_cgyjfkusd,"采购预计付款计划表-美元")],
    "yw_yc_dp":[(create_df_chycbj,"出货异常报警"),
                (create_df_ddycbj,"订单异常报警")],

}


DRP_TJ_CW_PROCESS_DICT = {

    "cw_cgtj_fkd_dp":[(create_df_k3cgfkds_rmb,"采购付款统计报表(人民币)"),
                      (create_df_k3cgfkds_mj,"采购付款统计报表(美金)")],
    "cw_kh_business_dp":[(create_df_k3jyqks_rmb, "经营情况统计(人民币)"),
                         (create_df_k3jyqks_mj, "经营情况统计(美金)")],
    "cw_xsqkfx_dp":[(create_df_xsqkfx_rmb, "销售情况分析表(人民币)"),
                    (create_df_xsqkfx_mj, "销售情况分析表(美金)")],
    "cw_yskfx_dp":[(create_df_kh_yskfx_rmb, "应收账款分析表(人民币)"),
                   (create_df_kh_yskfx_mj, "应收账款分析表(美金)")],

    "cw_skjllrhs_dp":[(create_df_skjllrhs,"收款奖励利润核算明细")],
    "cw_skfx_dp":[(create_df_skfx,"收账款分析通知表")],
    "cw_zhmx_dp":[(create_df_zhmx,"收款奖励利润核算明细表")],
    "cw_cpbfy_dp":[(create_df_cpbfy,"产品部任务及费用核算表")],
    "cw_xsbfy_dp":[(create_df_xsbfy,"销售部任务及费用核算表")]


}




################  3 合并数据报表配置  end #######################



